<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    public function index(){
        if (auth()->check()){
            return redirect(route('admin.index'));
        }
        return view('admin.login.login');
    }

    public function login(Request $request){
        if ($request->isMethod('post')){

            $post = $this->validate($request,[
                'username' => 'required',
                'password' => 'required',
            ],[
                'username.required' => '用户名不能为空',
                'password.required' => '密码不能为空',
            ]);

            $demo = auth()->attempt($post);

            if ($demo){
                return redirect(route('admin.index'));
            }else{
                return redirect(route('admin.login'))->withErrors(['error'=>'用户名或密码错误']);
            }

        }
    }
}
